package com.nettyrpc.bean;

import com.nettyrpc.protocol.RpcRequest;

public abstract class AbstractInvokeHandler implements InvokeHandler {


    public Object handle(Class<?> clazz, Object serviceBean, String methodName, Class<?>[] parameterTypes, Object[] parameters) throws Throwable {
        return doHandle(clazz, serviceBean, methodName, parameterTypes, parameters);
    }

    /**
     * 模板方法，交给子类实现
     * @param clazz
     * @param serviceBean
     * @param methodName
     * @param parameterTypes
     * @param parameters
     * @return
     * @throws Throwable
     */
    protected abstract Object doHandle(Class<?> clazz, Object serviceBean, String methodName, Class<?>[] parameterTypes, Object[] parameters) throws Throwable;

    @Override
    public Object handle(Object serviceBean, RpcRequest request) throws Throwable {
        Class<?> serviceClass = serviceBean.getClass();
        String methodName = request.getMethodName();
        Class<?>[] parameterTypes = request.getParameterTypes();
        Object[] parameters = request.getParameters();
        return handle(serviceClass, serviceBean, methodName, parameterTypes, parameters);
    }
}
